package com.feifan.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.feifan.api.dto.OmsReturnApplyQueryParam;
import com.feifan.api.dto.OmsUpdateStatusParam;
import com.feifan.api.model.OmsOrderReturnApply;
import com.feifan.api.model.OmsOrderReturnApplyExample;
import com.feifan.api.service.IOmsOrderReturnApplyService;
import com.feifan.common.CommonPage;
import com.feifan.provider.mapper.OmsOrderReturnApplyMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-08 09:17
 * @description: Functions of this class is
 **/
@Service(
        version = "1.0.0",
        interfaceName = "com.feifan.api.service.IOmsOrderReturnApplyService",
        interfaceClass = IOmsOrderReturnApplyService.class
)
@Transactional
public class OmsOrderReturnApplyService implements IOmsOrderReturnApplyService {

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private OmsOrderReturnApplyMapper dao;

    @Cacheable(cacheNames = "OrderReturnApplyList", unless = "#result==null",
            key = "T(String).valueOf(#pageNum" + '-' + "#pageSize)" +//要将其变成字符串，将其变成唯一的一些字符串
                    ".concat(#queryParam.id!=null?#queryParam.id:'id')" +
                    ".concat(#queryParam.receiverKeyword!=null?#queryParam.receiverKeyword:'receiverKeyword')" +
                    ".concat(#queryParam.status!=null?#queryParam.status:'status')" +
                    ".concat(#queryParam.createTime!=null?#queryParam.createTime:'createTime')" +
                    ".concat(#queryParam.handleMan!=null?#queryParam.handleMan:'handleMan')" +
                    ".concat(#queryParam.handleTime!=null?#queryParam.handleTime:'handleTime')"
    )//SpringEL表达式
    @Override
    public CommonPage list(OmsReturnApplyQueryParam queryParam, Integer pageNum, Integer pageSize) {
        //开始分页处理
        clearOrderReturnApply();
        PageHelper.startPage(pageNum, pageSize);

        return CommonPage.restPage(dao.getOrderReturnApplyList(queryParam));
    }

    @Cacheable(cacheNames = "OrderReturnApply", unless = "#result==null", key = "#id")
    @Override
    public OmsOrderReturnApply getItem(Long id) {
        return dao.selectByPrimaryKey(id);
    }

    @CacheEvict(cacheNames = "OrderReturnApplyList", key = "#id", allEntries = true, beforeInvocation = false)
    @Override
    public Integer delete(List<Long> ids) {
        clearOrderReturnApply();
        OmsOrderReturnApplyExample example = new OmsOrderReturnApplyExample();
        //找到要删除的那些申请订单
        example.createCriteria().andIdIn(ids).andStatusEqualTo(3);//同时限定只删除订单状态为已拒绝的
        return dao.deleteByExample(example);
    }

    @CachePut(cacheNames = "OrderReturnApply", key = "#id", unless = "#result==null")
    @Override
    public OmsOrderReturnApply updateStatus(Long id, OmsUpdateStatusParam status) {
        clearOrderReturnApply();
        Integer thisStatus = status.getStatus();//先把状态取出来 一共有三个状态1 2 3
        OmsOrderReturnApply returnApply = this.getItem(id);//先通过id把要修改的那条数据给取出来
        switch (thisStatus) {
            case 1: {//该状态为确认退货
                returnApply.setId(id);
                returnApply.setStatus(thisStatus);
                returnApply.setReturnAmount(status.getReturnAmount());
                returnApply.setCompanyAddressId(status.getCompanyAddressId());
                returnApply.setHandleTime(new Date());
                returnApply.setHandleMan(status.getHandleMan());
                returnApply.setHandleNote(status.getHandleNote());
                break;
            }
            case 2: {//该状态为完成退货
                returnApply.setId(id);
                returnApply.setStatus(thisStatus);
                returnApply.setReceiveTime(new Date());
                returnApply.setReceiveMan(status.getReceiveMan());
                returnApply.setReceiveNote(status.getReceiveNote());
                break;
            }
            case 3: {//该状态为拒绝退货
                returnApply.setId(id);
                returnApply.setStatus(thisStatus);
                returnApply.setHandleTime(new Date());
                returnApply.setHandleMan(status.getHandleMan());//处理人员
                returnApply.setHandleNote(status.getHandleNote());//处理备注
                break;
            }
            default: {
                return returnApply;
            }
        }
        dao.updateByPrimaryKeySelective(returnApply);//最后对数据库中的数据进行最后的修改
        return returnApply;
    }

    public void clearOrderReturnApply() {
        cacheManager.getCache("orderReturnApply").clear();//清空的带List的，即每次展示出的列表的缓存
    }
}